package com.sgb.kjwl.viewmodel.self.mine;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.service.UserAPI;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.mine.IntelligentAttentionActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IntelligentFocusModel extends BaseViewModel {
    SelfRepertory repertory;
    IntelligentAttentionActivity intelligentAttentionActivity;
    public IntelligentFocusModel(SelfRepertory repertory, IntelligentAttentionActivity intelligentAttentionActivity) {
        this.repertory = repertory;
        this.intelligentAttentionActivity = intelligentAttentionActivity;
    }

    //获取企业模块列表
    public void getDataList() {
        DialogHelper.showProgressDialog(intelligentAttentionActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getServiceList(UserAPI.UserCenterUrl.URL_ATTENTION_TYPES).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    // TODO: 2021/3/11   java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.LinkedHashMap
                    Map<String,String> dataMap = (Map<String, String>) response.body().getData();
                    intelligentAttentionActivity.getDataList(dataMap);
                } else {
                    MToast.showLongToast(intelligentAttentionActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                intelligentAttentionActivity.getDataList(new HashMap<>());
            }
        });
    }

}
